<?php
/**
 * 花集API接口
 *
 */
class HJClient {

    public $readTimeout = 10, $connectTimeout = 10;
    public $gatewayUrl ='http://cpapi.huaj.net';
    public $format = 'json'; //json & xml
    public $checkRequest = true;
    protected $apiUrl = array();
    const SIGN_KEY = '12#@!';
    /**
     * 得到api接口数据
     *
     * @param string $method
     * @param array $paras
     *
     * @return mixed
     * @throws Exception
     */
    public function api($method, $params = array(),$m = 'get') {
        $url = $this->gatewayUrl.$method;
        $params['sign'] = $this->sign($params);
        if($m == 'post'){
            $content = $this->send($url, $params);
        }else{
            echo $url.'?'.http_build_query($params);
            $content = $this->send($url.'?'.http_build_query($params));
        }
        $result = json_decode($content,true);
        return $result;
    }

    private function sign($params){
        ksort($params);
        $strs = array();
        foreach($params as $k =>$v){
            $strs[] = $k.'='.$v;
        }
        return md5(HJClient::SIGN_KEY.implode("",$strs).HJClient::SIGN_KEY);
    }
    /**
     * 发送请求
     *
     * @param      $url
     * @param null $postFields
     *
     * @return mixed
     * @throws Exception
     */
    public function send($url, $postFields = null) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($this->readTimeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
        }
        if ($this->connectTimeout) {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
        }
        //https 请求
        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSLVERSION, 3);
        }

        if (is_array($postFields) && 0 < count($postFields)) {
            $postBodyString = "";
            $postMultipart = false;
            foreach ($postFields as $k => $v) {
                if ("@" != substr($v, 0, 1)) {//判断是不是文件上传
                    $postBodyString .= "$k=" . urlencode($v) . "&";
                } else {//文件上传用multipart/form-data，否则用www-form-urlencoded
                    $postMultipart = true;
                }
            }
            unset($k, $v);
            curl_setopt($ch, CURLOPT_POST, true);
            if ($postMultipart) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
            } else {
                curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
            }
        }
        $response = curl_exec($ch);
        echo $response;
        if (curl_errno($ch)) {
            throw new Exception(1001, "API调用异常\n" . $url . "\n" . curl_error($ch));
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                throw new Exception($httpStatusCode, $response);
            }
        }
        curl_close($ch);
        return $response;
    }
}
